/*
 * mem_wrap.c
 *
 * Copyright (C) 2014 Ingenic Semiconductor Co.,Ltd
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <sys_wrap/mem_wrap.h>
#include <sys_wrap/jz_json.h>
#include <json.h>
#ifdef DMALLOC
#include <imp/dmalloc.h>
#endif

int JsonCheck = -1;

//Current function and line about the "malloc" use
char *imp_malloc(size_t object_size, const char *funcs, int lines)
{
	char *my_buf = NULL;
	my_buf = malloc(object_size);

	if(!JsonCheck){
		printf("[malloc memory]function: %s; line: %d\t\n[malloc infos] size: %d, address: 0x%x\n", funcs, lines, (int)object_size, my_buf);
	}

	return my_buf;
}

//Current function and line about the "calloc" use
char *imp_calloc(size_t nmemb, size_t memb_size, const char *funcs, int lines)
{
	char *my_buf = NULL;
	my_buf = calloc(nmemb, memb_size);

	if(!JsonCheck){
		printf("[calloc memory]function: %s; line: %d\t\n", funcs, lines);
		printf("[calloc infos] size: %d, address: 0x%x\n", (int)(nmemb * memb_size), my_buf);
	}

	return my_buf;
}

//Current function and line about the "realloc" use
char *imp_realloc(void *ptr, size_t object_size, const char *funcs, int lines)
{
	char *my_buf = NULL;
	my_buf = realloc(ptr, object_size);

	if(!JsonCheck){
		printf("[realloc memory]function: %s; line: %d\t\n", funcs, lines);
		printf("[realloc infos] size: %d, address: 0x%x\n", (int)object_size, my_buf);
	}

	return my_buf;
}

//Current function and line about the "free" use
void imp_free(void *ptr, const char *funcs, int lines)
{
	free(ptr);

	if(!JsonCheck){
		printf("[free memory]function: %s; line: %d\t\n", funcs, lines);
		//printf("[free infos] address: 0x%x\n", ptr);
	}
}

//Initialize the memory control
int sys_wrap(char *debug)
{
	JsonCheck = json_func(debug);

	return JsonCheck;
}

